﻿using System;

using Cookbook.Services.Core.Commands;
using Cookbook.Services.Core.Domain;
using Cookbook.Services.Core.Persistency;

namespace Cookbook.Services.Core.Handlers
{
    public class AddNewRecipeHandler : IHandles<AddNewRecipeCommand>
    {
        private readonly Func<CookbookUnitOfWork> unitOfWorkFactory;

        public AddNewRecipeHandler(Func<CookbookUnitOfWork> unitOfWorkFactory)
        {
            this.unitOfWorkFactory = unitOfWorkFactory;
        }

        public void Handle(AddNewRecipeCommand command)
        {
            using (var uow = unitOfWorkFactory())
            {
                uow.Recipes.Add(new Recipe(command.Title, command.Description));
            }
        }
    }
}
